package jp.co.sharp.android.parents.kidsguard.activity.bluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import jp.co.sharp.android.parents.kidsguard.App;
import jp.co.sharp.android.parents.kidsguard.R;
import jp.co.sharp.android.parents.kidsguard.activity.home.HomeActivity;
import jp.co.sharp.android.parents.kidsguard.common.OpenBluetoothConfirmDlg;
import jp.co.sharp.android.parents.kidsguard.contants.Constants;
import jp.co.sharp.android.parents.kidsguard.entity.KidInfoEntity;

/**
 * ようこそ
 */
public class BluetoothActivity extends Activity {

    private TextView mBtnPageNext;
    private static final int TIME_EXIT=2000;
    private long mBackPressed;
    private boolean mUnReceiver = false;

    private static final String TAG = "TAG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(this.TAG, "BluetoothActivity_onCreate");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(this.TAG, "BluetoothActivity_onStart");
        initReceiver();
        initActivity();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(this.TAG, "BluetoothActivity_onStop");
        if (!mUnReceiver) {
            unregisterReceiver(receiver);
            mUnReceiver = true;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(this.TAG, "BluetoothActivity_onDestroy");
        if (!mUnReceiver) {
            unregisterReceiver(receiver);
            mUnReceiver = true;
        }
    }

    /**
     *
     */
    private void initActivity() {
        Log.i(this.TAG, "BluetoothActivity_initActivity");
        //
        int kidCnt = ((App) getApplication()).getMaxKidInfo();
        if (kidCnt > 0) {
            // Update Kid State TODO
            List<KidInfoEntity> lsKidInfoEntity = ((App) getApplication()).getCacheEntity().getKidLists();
            for (KidInfoEntity kidInfoEntity : lsKidInfoEntity) {
                kidInfoEntity.setBtConnStatus(Constants.DEVICE_DISCONNECTION_SUCCESS);
                kidInfoEntity.setControlled(false);
                ((App) getApplication()).updateKidInfo(kidInfoEntity);
            }
            Intent intent1 = new Intent();
            intent1.setClass(BluetoothActivity.this, HomeActivity.class);
            intent1.setAction(Constants.ACTION_INIT_HOME);
            startActivity(intent1);
        } else {
            setContentView(R.layout.activity_bluetooth);

            // 次へ
            mBtnPageNext = (TextView)findViewById(R.id.btnPageNext);
            mBtnPageNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (checkBluetoothState()) {
                        Intent intent = new Intent();
                        intent.setClass(BluetoothActivity.this, BluetoothSearchActivity.class);
                        startActivity(intent);
                    } else {
                        OpenBluetoothConfirmDlg openBluetoothConfirmDlg = new OpenBluetoothConfirmDlg(BluetoothActivity.this, R.style.commonDlg);
                        openBluetoothConfirmDlg.show();
                        openBluetoothConfirmDlg.setOnOperateSetListener(new OpenBluetoothConfirmDlg.OnOperateSetListener() {
                            @Override
                            public void onOperateSet() {
                                openBluetoothConfirmDlg.dismiss();
                                startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS)); //直接进入手机中的蓝牙设置界面
                            }
                        });
                    }
                }
            });

            // 詳細情報
            TextView showDetailInfo = (TextView)findViewById(R.id.showDetailInfo);
            showDetailInfo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    TextView detailInfo = findViewById(R.id.detail_info);
                    ImageView imageView = (ImageView)findViewById(R.id.imgDownArrow);
                    if (detailInfo.getVisibility() == View.VISIBLE) {
                        detailInfo.setVisibility(View.INVISIBLE);
                        imageView.setImageResource(R.mipmap.bluetooth_down_arrow_24px);
                    } else {
                        detailInfo.setVisibility(View.VISIBLE);
                        imageView.setImageResource(R.mipmap.bluetooth_up_arrow_24px);
                    }
                }
            });
        }
    }

    /**
     * 初期化Receiver
     */
    private void initReceiver() {
        Log.i(this.TAG, "BluetoothActivity_initReceiver");
        IntentFilter filter = new IntentFilter();
        // Bluetooth
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(receiver, filter);
    }

    /**
     * BroadcastReceiver
     */
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                String action = intent.getAction();
                if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                    int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                    if (BluetoothAdapter.STATE_OFF == blueState) {
                        mBtnPageNext.setTextColor(getResources().getColor(R.color.colorConnBtn));
                    } else if (BluetoothAdapter.STATE_ON == blueState) {
                        mBtnPageNext.setTextColor(getResources().getColor(R.color.colorDisConnBtn));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    /**
     * bluetooth status
     * @return true: Open  false: Close
     */
    private boolean checkBluetoothState() {
        Log.i(this.TAG, "BluetoothActivity_checkBluetoothState");
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (null != bluetoothAdapter) {
            if (bluetoothAdapter.isEnabled()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void onBackPressed(){
        Log.i(this.TAG, "BluetoothActivity_onBackPressed");
        if(mBackPressed + TIME_EXIT > System.currentTimeMillis()){
            super.onBackPressed();
            return;
        }else{
            Toast.makeText(this,"もう一度押すとアプリケーションを終了します",Toast.LENGTH_SHORT).show();
            mBackPressed=System.currentTimeMillis();
        }
    }
}
